package com.bidevalution.model;

import com.bidevalution.core.Common;
import com.bidevalution.core.enums.BlNodeEnum;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.OConvertUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Date;

/**
 * 推送標錄的數據格式
 * @author huyiqi
 * @date 2019/11/16
 */
public class BlData {

    /**
     * 屬性值
     */
    private static final String ATTR_VALUE_ENCODING = "gb2312";

    private static final String ATTR_KEY_PROJECTNO = "ProjectNo";
    private static final String ATTR_KEY_ORGANIZATIONCODE = "OrganizationCode";
    private static final String ATTR_KEY_NAME = "Name";
    private static final String ATTR_KEY_BIDPRICE = "BidPrice";
    private static final String ATTR_KEY_TIME = "Time";
    private static final String ATTR_KEY_QUALITY = "Quality";
    private static final String ATTR_KEY_REMARK = "Remark";
    private static final String ATTR_KEY_CREATETIME = "CreateTime";


    private Document document;
    private Element root;
    private String sectionCode;

    public BlData(String sectionCode) {
        //初始化xml對象，添加root節點
        document = DocumentHelper.createDocument();
        document.setXMLEncoding(ATTR_VALUE_ENCODING);
        root = document.addElement(BlNodeEnum.ROOT.getNode());

        this.sectionCode = sectionCode;
    }

    public BlData addRow(String orgCode, String name, Double bidPrise, String time, String quality, String remark
            , Date createTime) {
        Element row = root.addElement(BlNodeEnum.ROW.getNode());
        row.addAttribute(ATTR_KEY_PROJECTNO, sectionCode);
        row.addAttribute(ATTR_KEY_ORGANIZATIONCODE, orgCode);
        row.addAttribute(ATTR_KEY_NAME, name);
        row.addAttribute(ATTR_KEY_BIDPRICE, OConvertUtils.getString(bidPrise));
        row.addAttribute(ATTR_KEY_TIME, time);
        row.addAttribute(ATTR_KEY_QUALITY, quality);
        row.addAttribute(ATTR_KEY_REMARK, remark);
        row.addAttribute(ATTR_KEY_CREATETIME, DateUtil.dateToString(createTime, 2));

        return this;
    }

    @Override
    public String toString() {
        return Common.xmlToString(document, ATTR_VALUE_ENCODING);
    }
}
